package com.market.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.annotation.AdminLogAnnotation;
import com.market.bean.MarketLog;
import com.market.bean.MarketLogExample;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.CommonData;
import com.market.mapper.MarketLogMapper;
import com.market.service.admin.AdminLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 2731235517@qq.com 谢畅
 * @since 2022/12/29 17:45
 */
@Transactional
// @AdminLogAnnotation
@Service
public class AdminLogServiceImpl implements AdminLogService {
    @Autowired
    MarketLogMapper marketLogMapper;

    @Override
    public CommonData<MarketLog> list(BasePageInfo basePageInfo, String name) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        MarketLogExample marketLogExample = new MarketLogExample();
        MarketLogExample.Criteria criteria = marketLogExample.createCriteria();
        // 如果不为空，则拼接条件
        if (!StringUtils.isEmpty(name)) {
            criteria.andAdminLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);
        // 根据sort做order排序
        marketLogExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());

        List<MarketLog> marketLogList = marketLogMapper.selectByExample(marketLogExample);
        PageInfo<MarketLog> pageInfo = new PageInfo<>(marketLogList);
        return CommonData.data(pageInfo);
    }
    @AdminLogAnnotation
    @Override
    public void createLog(MarketLog marketLog) {
        marketLogMapper.insert(marketLog);
    }
}
